﻿using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys.Farmings;
using MediatR;

namespace B.S.RbacData.Read.API.Applications.CommandHandlters.Traceabilitys.Farmings
{
    /// <summary>
    /// 获取养殖信息命令处理
    /// </summary>
    public class GetFarmingCommandHandlter : IRequestHandler<GetFarmingCommand, ApiResult<List<FarmingModel>>>
    {
        /// <summary>
        /// 养殖信息仓储
        /// </summary>
        private readonly IBaseRepository<FarmingModel> farmingRep;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="farmingRep">养殖信息仓储</param>
        public GetFarmingCommandHandlter(IBaseRepository<FarmingModel> farmingRep)
        {
            this.farmingRep = farmingRep;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<List<FarmingModel>>> Handle(GetFarmingCommand request, CancellationToken cancellationToken)
        {

            //根据溯源编号查询养殖信息
            var farminglist=farmingRep.GetAll().Where(x=>x.ProductId==request.ProductId).OrderByDescending(x=>x.FarmingId).ToList();


            return new ApiResult<List<FarmingModel>>
            {
                code = ResultEnumCode.Ok,
                data = farminglist,
                message = "获取养殖信息成功"

            };
        }
    }
}
